/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 3/2/14
 * Time: 11:42 AM
 * To change this template use File | Settings | File Templates.
 */
package com.societyGames.codeAlong.analytics
{
import com.gameanalytics.GameAnalytics;
import com.societyGames.codeAlong.state.codeExercise.model.ModuleData;
import com.societyGames.codeAlong.user.UserData;

import flash.display.LoaderInfo;
import flash.system.Capabilities;

/**
 * Wrapper around analytics.
 */
public class Analytics
{
  /**
   * Prepare analytics for use.
   */
  public static function initialize(userId:String, canUseSharedObject:Boolean, loaderInfo:LoaderInfo):void
  {
    const SECRET_KEY:String = "45ccbc5ba00663747ac84bb07dbd00ba381b2088";
    const GAME_KEY:String = "04d56940b754c17dfbbb1c673b1d9672";
    const GAME_VERSION:String = UserData.MAJOR_VERSION.toString() + "." + UserData.MINOR_VERSION.toString();
    GameAnalytics.init(SECRET_KEY, GAME_KEY, GAME_VERSION, null, userId, canUseSharedObject);
    //GameAnalytics.catchUnhandledExceptions(loaderInfo, true);
  }

  /**
   * Scrap anything related to old analytics.
   */
  public static function clearAll():void
  {
    GameAnalytics.deleteAllEvents();
  }

  public static function from(source:String):void
  {
    GameAnalytics.newDesignEvent("from:" + source, 1);
  }

  public static function view(moduleData:ModuleData):void
  {
    GameAnalytics.newDesignEvent("lesson:" + moduleData.id.toString() + ":view", 1, moduleData.id.toString());
  }

  public static function compile(moduleData:ModuleData):void
  {
    GameAnalytics.newDesignEvent("lesson:" + moduleData.id.toString() + ":compile", 1, moduleData.id.toString());
  }

  public static function syntaxError(moduleData:ModuleData, syntaxError:SyntaxError):void
  {
    GameAnalytics.newDesignEvent("lesson:" + moduleData.id.toString() + ":syntaxError:" + syntaxError.errorID.toString(), 1, moduleData.id.toString());
  }

  public static function exception(missionId:uint, testCaseId:int, error:Error):void
  {
    GameAnalytics.newDesignEvent("lesson:" + missionId.toString() + ":exception:" + testCaseId.toString() + ":" + error.errorID.toString(), 1, missionId.toString());
  }

  public static function unhandledException(missionId:uint, testCaseId:int):void
  {
    GameAnalytics.newDesignEvent("lesson:" + missionId.toString() + ":unhandledException:" + testCaseId.toString(), 1, missionId.toString());
  }

  public static function failTestCase(missionId:uint, testCaseId:int):void
  {
    GameAnalytics.newDesignEvent("lesson:" + missionId.toString() + ":failTestCase:" + testCaseId.toString(), 1, missionId.toString());
  }

  public static function succeedTestCase(missionId:uint, testCaseId:int):void
  {
    GameAnalytics.newDesignEvent("lesson:" + missionId.toString() + ":succeedTestCase:" + testCaseId.toString(), 1, missionId.toString());
  }

  public static function succeedAllFirstTime(missionId:uint):void
  {
    GameAnalytics.newDesignEvent("lesson:" + missionId.toString() + ":succeedAllFirstTime", 1, missionId.toString());
  }

  public static function succeedAll(missionId:uint):void
  {
    GameAnalytics.newDesignEvent("lesson:" + missionId.toString() + ":succeedAll", 1, missionId.toString());
  }

  public static function error(missionId:uint, message:String, severity:String):void
  {
    GameAnalytics.newErrorEvent(message, severity, missionId.toString());
  }

  public static function dumpInformation():void
  {
    trace("User id: " + GameAnalytics.getUserId() + "\n");
    trace("Session id: " + GameAnalytics.getSessionId() + "\n");
    trace("OS: " + Capabilities.os + "\n");
    trace("Manufacturer: " + Capabilities.manufacturer + "\n");
    trace("Version: " + GameAnalytics.getVersion() + "\n");
  }
}
}
